<?php
namespace app\index\controller;

use app\index\model\AccountListModel;
use app\index\model\BidModel;
use app\index\model\BidPoolModel;
use app\index\model\CertBorrowListModel;
use app\index\model\CertBorrowModel;
use app\index\model\CertModel;
use app\index\model\CertPersonModel;
use app\index\model\CertUseBidModel;
use app\index\model\CertUseModel;
use app\index\model\ContractBianModel;
use app\index\model\ContractBuModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractShiModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\DeviceJieModel;
use app\index\model\DeviceLinModel;
use app\index\model\EnginBondFanModel;
use app\index\model\EnginBondJiaoModel;
use app\index\model\EnginBondKaiModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginChanModel;
use app\index\model\EnginModel;
use app\index\model\EnginSpeedListModel;
use app\index\model\EnginXunModel;
use app\index\model\EnginZhengModel;
use app\index\model\FlowBillModel;
use app\index\model\IncomeEnginModel;
use app\index\model\LabourLingModel;
use app\index\model\LabourShenModel;
use app\index\model\PaymentBaoModel;
use app\index\model\PaymentDayModel;
use app\index\model\PaymentEnginModel;
use app\index\model\PaymentGongListModel;
use app\index\model\PaymentGongModel;
use app\index\model\PaymentJieModel;
use app\index\model\PaymentNeifuModel;
use app\index\model\PaymentNeikouModel;
use app\index\model\PiaoKaiListModel;
use app\index\model\PiaokaiModel;
use app\index\model\PiaoShouListModel;
use app\index\model\PiaoShouModel;
use app\index\model\SpeedZongListModel;
use app\index\model\SpeedZongModel;
use app\index\model\StuffJieModel;
use app\index\model\SubbaoShenModel;
use app\index\model\UserModel;
use app\index\service\BidService;
use app\index\service\BudgetService;
use app\index\service\CertService;
use app\index\service\EnginService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\PaymentService;
use app\index\service\PiaoService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Home extends Common
{

    public function engin_chan(Request $request){
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];


        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }
        $whereChan = [];
        if($chanid_pid > 0){
            $whereChan[] = ['chanid','in', $chanid_son];
        }


        $enginIdArr = EnginModel::where($where)->where($whereTurn)->where($whereChan)->where((new MenuService())->get_miss_where('homeengin', ''))->column('id');
        $data['tong_all'] = count($enginIdArr);
        $data['tong_zai'] = EnginModel::where($where)->where($whereTurn)->where($whereChan)->where((new MenuService())->get_miss_where('homeengin', ''))->where(['state'=>3])->count();
        $data['tong_shimoney'] = 0;


        $data['token_roletype'] = $this->userinfo['token_roletype'];
        $data['xianname'] = '';
        if($this->userinfo['token_roletype'] == 2){
            $data['xianname'] = '项目人员';
        }
        if($this->userinfo['token_roletype'] == 3){
            $lianerarr = DealcomModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'isturn'=>1,'status'=>1])
                ->where('id','in',$this->userinfo['lianer'])->column('name');
            if(count($lianerarr) <= 2){
                $data['xianname'] = implode(' / ', $lianerarr);
            }else{
                $data['xianname'] = $lianerarr[0].' / '.$lianerarr[1].' ......';
            }

        }



        $shiinfo = ContractShiModel::field('id,money')->where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->select()->toArray();
        foreach ($shiinfo as $vv){
            $data['tong_shimoney'] += $vv['money'];
            $data['tong_shimoney'] += ContractBuModel::where($where)->where($whereTurn)->where(['shiid'=>$vv['id']])->sum('money');
        }
        $data['tong_chan'] = EnginChanModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('leijin');


        $data['tong_piaokai'] = PiaokaiModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)
            ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');

        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);
        $data['tong_huikuan'] = IncomeEnginModel::where($where)->where('enginid','in',$enginIdArr)->where('budid','in',$budshou)->sum('money');
        $data['tong_nohuikuan'] = $data['tong_chan'] - $data['tong_huikuan'];


        $data['tong_caimoney'] = ContractCaiModel::where($where)->where($whereTurn)
            ->where('enginid','in',$enginIdArr)->sum('money');



        $data['tong_jie'] = StuffJieModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('money');
        $data['tong_jie'] += LabourShenModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('money');
        $data['tong_jie'] += SubbaoShenModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('money');
        $data['tong_jie'] += DeviceJieModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('money');
        $data['tong_jie'] += LabourLingModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('money');
        $data['tong_jie'] += DeviceLinModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('money');

        $data['tong_neifu'] = PaymentNeifuModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('money');


        $data['tong_piaoshou'] = PiaoShouModel::where($where)->where('enginid','in',$enginIdArr)->sum('money');
        $data['tong_payment_engin'] = PaymentEnginModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('money');
        $data['tong_payment_bao'] = PaymentBaoModel::where($where)->where($whereTurn)->where('enginid','in',$enginIdArr)->sum('money');
        $data['tong_cha'] = $data['tong_huikuan'] - $data['tong_payment_engin'] - $data['tong_payment_bao'];


        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = $chanid;
        $data['enginidstr'] = implode(',', $enginIdArr);
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function engin_dong(Request $request){
        $dt = $request->param('dt', '', 'string');
        $dt = empty($dt) ? date('Y-m-d') : $dt;

        $enginidstr = $request->param('enginidstr', '', 'string');
        if(empty($enginidstr)){
            $enginidstr = '-1';
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];
        $where[] = ['created_at','between', [$dt.' 00:00:00', $dt.' 23:59:59']];

        $list = FlowBillModel::where($where)->where(['isturn'=>1])->where('status','in',[0,1])
            ->where('enginid','in',$enginidstr)->order('created_at DESC')->select()->toArray();
        foreach ($list as &$vvv){
            $vvv['created_time'] = date("H:i", strtotime($vvv['created_at']));
        }

        $data['data'] = $list;
        $data['isdt'] = $dt;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function engin_dan(Request $request){
        $enginidstr = $request->param('enginidstr', '', 'string');
        $enginid = $request->param('enginid', 0, 'int');
        $engininfo = [];

        $enginlist = EnginModel::field('*,id as value')->where('id', 'in', $enginidstr)->order('id DESC')->select()->toArray();
        if(!$enginid){
            $enginid = isset($enginlist[0]['id']) ? $enginlist[0]['id'] : 0;
            $engininfo = isset($enginlist[0]) ? $enginlist[0] : [];
        }else{
            $engininfo = EnginModel::where(['id'=>$enginid])->find();
        }
        $data['enginlist'] = $enginlist;
        $data['is_enginid'] = $enginid;
        $data['is_enginidname'] = EnginModel::where(['id'=>$enginid])->value('name');
        $data['isinfo'] = false;
        if($engininfo == []){
            return outputJson(2000,lang('code_2000'),$data);
        }

        $data['isinfo'] = true;
        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];

        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $whereEngin[] = ['enginid','=', $engininfo['id']];

        $data['gai_business_name'] = $engininfo['business'] == 1 ? '自营' : '联营';
        $data['gai_state_name'] = (new EnginService())->get_engin_state_name($engininfo['id'], $engininfo['state']);
        $data['gai_chanid_name'] = DeptModel::where(['id'=>$engininfo['chanid']])->value('name');
        $data['gai_jian_unit'] = $engininfo['jian_unit'];
        $data['gai_man_xiang_name'] = UserModel::get_realname_by_pk($engininfo['man_xiang']);




        $data['income_shimoney'] = (new EnginService())->get_contract_shi_final_money($engininfo['id']);
        $data['income_chan'] = EnginChanModel::where($where)->where($whereTurn)->where($whereEngin)->sum('leijin');
        $data['income_piaokai'] = PiaokaiModel::where($where)->where($whereTurn)->where($whereEngin)
            ->where("dt is not null and dt != 0000-00-00 and dt != ''")->sum('money');
        $gckid = (new BudgetService())->get_budshou_pid_by_name('项目收入','工程款');
        $budshou = (new BudgetService())->get_budshou_sons($gckid);
        $data['income_huikuan'] = IncomeEnginModel::where($where)->where($whereEngin)->where('budid','in',$budshou)->sum('money');
        $data['income_chanhui'] = 0;
        if($data['income_chan'] != 0){
            $data['income_chanhui'] = round($data['income_huikuan']/$data['income_chan']*100, 2);
        }
        $dzkid = (new BudgetService())->get_budshou_pid_by_name('项目收入','垫资款');
        $budshouDian = (new BudgetService())->get_budshou_sons($dzkid);
        $data['income_diankuan'] = IncomeEnginModel::where($where)->where($whereEngin)->where('budid','in',$budshouDian)->sum('money');
        $data['income_ying_chan'] = $data['income_chan'] - $data['income_huikuan'];
        $data['income_ying_kai'] = $data['income_piaokai'] - $data['income_huikuan'];


        $data['payment_caimoney'] = ContractCaiModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['payment_stuffjie'] = StuffJieModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['payment_labourshen'] = LabourShenModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['payment_subbaoshen'] = SubbaoShenModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['payment_devicejie'] = DeviceJieModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['payment_labourling'] = LabourLingModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['payment_devicelin'] = DeviceLinModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['payment_leijie'] = round($data['payment_stuffjie']+$data['payment_labourshen']+$data['payment_subbaoshen']+
            $data['payment_devicejie']+$data['payment_labourling']+$data['payment_devicelin'], 2);
        $data['payment_payengin'] = PaymentEnginModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['payment_paybao'] = PaymentBaoModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['payment_piaoshou'] = PiaoShouModel::where($where)->where($whereEngin)->sum('money');
        $data['payment_ying_jie'] = $data['payment_leijie'] - $data['payment_payengin'];
        $data['payment_ying_shou'] = $data['payment_piaoshou'] - $data['payment_payengin'] - $data['payment_paybao'];



        $data['cheng_budget'] = EnginBudgetModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['cheng_caimoney'] = $data['payment_caimoney'];
        $data['cheng_yubi'] = 0;
        if($data['cheng_budget'] != 0){
            $data['cheng_yubi'] = round($data['cheng_caimoney']/$data['cheng_budget']*100, 2);
        }
        $data['cheng_leijie'] = $data['payment_leijie'];
        $data['cheng_fukuan'] = round($data['payment_payengin'] + $data['payment_paybao']);

        $data['cheng_cai_cai'] = ContractCaiModel::where($where)->where($whereTurn)->where($whereEngin)->where(['caihetype'=>1])->sum('money');
        $data['cheng_cai_lao'] = ContractCaiModel::where($where)->where($whereTurn)->where($whereEngin)->where(['caihetype'=>2])->sum('money');
        $data['cheng_cai_zu'] = ContractCaiModel::where($where)->where($whereTurn)->where($whereEngin)->where(['caihetype'=>3])->sum('money');
        $data['cheng_cai_fen'] = ContractCaiModel::where($where)->where($whereTurn)->where($whereEngin)->where(['caihetype'=>4])->sum('money');
        $data['cheng_cai_qi'] = ContractCaiModel::where($where)->where($whereTurn)->where($whereEngin)->where(['caihetype'=>5])->sum('money');

        $data['cheng_jie_stuff'] = $data['payment_stuffjie'];
        $data['cheng_jie_labour'] = round($data['payment_labourshen'] + $data['payment_labourling'], 2);
        $data['cheng_jie_device'] = round($data['payment_devicejie'] + $data['payment_devicelin'], 2);
        $data['cheng_jie_subbao'] = $data['payment_subbaoshen'];

        $data['cheng_fu_stuff'] = PaymentEnginModel::where($where)->where($whereTurn)->where($whereEngin)->where(['budid'=>1])->sum('money');
        $data['cheng_fu_labour'] = PaymentEnginModel::where($where)->where($whereTurn)->where($whereEngin)->where(['budid'=>2])->sum('money');
        $data['cheng_fu_device'] = PaymentEnginModel::where($where)->where($whereTurn)->where($whereEngin)->where(['budid'=>3])->sum('money');
        $data['cheng_fu_subbao'] = PaymentEnginModel::where($where)->where($whereTurn)->where($whereEngin)->where(['budid'=>4])->sum('money');
        $data['cheng_fu_cuo'] = PaymentEnginModel::where($where)->where($whereTurn)->where($whereEngin)->where(['budid'=>5])->sum('money');
        $data['cheng_fu_bao'] = $data['payment_paybao'];


        $data['xian_daofee'] = IncomeEnginModel::where($where)->where($whereEngin)->sum('money');
        $payenginId = PaymentEnginModel::where($where)->where($whereTurn)->where($whereEngin)->column('id');
        $data['xian_zhifee'] =  AccountListModel::where(['type'=>3])->where('mid','in',$payenginId)->sum('money');
        $baoId = PaymentBaoModel::where($where)->where($whereTurn)->where($whereEngin)->column('id');
        $data['xian_zhifee'] +=  AccountListModel::where(['type'=>4])->where('mid','in',$baoId)->sum('money');
        $neifuId = PaymentNeifuModel::where($where)->where($whereTurn)->where($whereEngin)->column('id');
        $data['xian_zhifee'] +=  AccountListModel::where(['type'=>6])->where('mid','in',$neifuId)->sum('money');
        $data['xian_yufee'] = round($data['xian_daofee'] - $data['xian_zhifee'], 2);





        $data['piao_kai'] = $data['income_piaokai'];
        $data['piao_xiao'] =  PiaoKaiListModel::alias('list')
            ->join('piao_kai zhu','zhu.id=list.kaiid')
            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
            ->where(['zhu.enginid'=>$engininfo['id']])
            ->sum('list.ratefee');
        $data['piao_shou'] = $data['payment_piaoshou'];
        $data['piao_jin'] = PiaoShouListModel::alias('list')
            ->join('piao_shou zhu','zhu.id=list.shouid')
            ->where(['zhu.deleted_user'=>0,'zhu.isturn'=>1,'zhu.status'=>1])
            ->where(['zhu.enginid'=>$engininfo['id']])
            ->where(['list.piaotype'=>1])
            ->sum('list.ratefee');
        $data['piao_ying'] = round($data['piao_xiao'] - $data['piao_jin'], 2);


        $data['jin_chanfee'] = 0;
        $data['jin_wanchan'] = 0;
        $data['jin_wanbi'] = 0;
        $data['jin_actualdt'] = $engininfo['actualdt'];
        $data['jin_shiday'] = 0;
        if($data['jin_actualdt'] && $data['jin_actualdt'] != '0000-00-00'){
            $junenddt = date("Y-m-d");
            if($engininfo['filishdt'] && $engininfo['filishdt'] != '0000-00-00'){
                $junenddt = $engininfo['filishdt'];
            }
            $data['jin_shiday'] = round( (strtotime($junenddt) - strtotime($data['jin_actualdt'])) / 86400 );
            $data['jin_shiday'] = $data['jin_shiday'] > 0 ? $data['jin_shiday'] : 0;
        }
        $speedZong = SpeedZongModel::where($where)->where($whereEngin)->find();
        if($speedZong){
            $data['jin_chanfee'] = $speedZong['chanfee'];
            $zongList = SpeedZongListModel::where(['zongid'=>$speedZong['id']])->select()->toArray();
            foreach ($zongList as $v){
                $shinum = EnginSpeedListModel::alias('list')
                    ->join('engin_speed speed','speed.id=list.speedid')
                    ->where(['speed.deleted_user'=>0,'speed.isturn'=>1,'speed.status'=>1])
                    ->where(['speed.enginid'=>$engininfo['id'],'list.zongid'=>$v['id']])
                    ->sum('list.shinum');
                $data['jin_wanchan'] += $shinum*$v['price'];
            }
        }
        if($data['jin_chanfee'] != 0){
            $data['jin_wanbi'] = round($data['jin_wanchan'] / $data['jin_chanfee'] *100, 2);
        }



        $data['xun_xunci'] = EnginXunModel::where($where)->where($whereEngin)->count();
        $data['xun_zhengci'] = EnginZhengModel::where($where)->where($whereEngin)->count();
        $data['xun_yizheng'] = EnginZhengModel::where($where)->where($whereEngin)->where(['gaista'=>1])->count();
        $data['xun_daizheng'] = EnginZhengModel::where($where)->where($whereEngin)->where(['gaista'=>0])->count();
        $data['xun_yuzheng'] = EnginZhengModel::where($where)->where($whereEngin)->where(['gaista'=>0])->where('dt', '<', date("Y-m-d"))->count();


        $data['nei_lian_unit'] = $engininfo['lian_unit'];
        $data['nei_xiangshou'] = $data['xian_daofee'];
        $data['nei_kou'] = PaymentNeikouModel::where($where)->where($whereEngin)->sum('money');
        $data['nei_fu'] = PaymentNeifuModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['nei_gongfu'] = PaymentEnginModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['nei_bao'] = PaymentBaoModel::where($where)->where($whereTurn)->where($whereEngin)->sum('money');
        $data['nei_keyong'] = round($data['nei_xiangshou'] - $data['nei_kou'] - $data['nei_fu'] -
            $data['nei_gongfu'] - $data['nei_bao'], 2);

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function engin_dan_xuan(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['e.deleted_user','=', 0];
        $where[] = ['e.comid','=',$this->userinfo['comid']];
        $where[] = ['e.status','=', 1];
        $where[] = ['e.isturn','=', 1];

        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['e.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['e.number','like', '%'.$param['number'].'%'];
        }

        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }
        $whereChan = [];
        if($chanid_pid > 0){
            $whereChan[] = ['e.chanid','in', $chanid_son];
        }


        $data['data'] = EnginModel::alias('e')
            ->field('e.*')
            ->where($where)
            ->where($whereChan)
            ->where((new MenuService())->get_miss_where('homeengin', 'e'))
            ->order('e.created_at DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['chanid_name'] = DeptModel::get_name_by_pk($item['chanid']);
                $item['man_xiang_name'] = UserModel::get_realname_by_pk($item['man_xiang']);
                $item['man_xiang_tel'] = UserModel::where(['id'=>$item['man_xiang']])->value('tel');
                $item['business_name'] = $item['business'] == 1 ? '自营' : '联营';
                $item['state_name'] = (new EnginService())->get_engin_state_name_tag($item['id'], $item['state']);
                $item['isting'] = $item['state'] == 3 ? true : false;

                $item['engintype_old'] = $item['engintype'];
                $item['engintype'] = (new OptService())->get_base_showname('engin_engintype', $item['engintype']);
                $item['zhaotype'] = (new OptService())->get_base_showname('bid_bidding_type', $item['zhaotype']);
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function cai_chan(Request $request){
        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }


        $stadt = date('Y-m-01');
        $enddt = date('Y-m-d', strtotime("$stadt +1 month -1 day"));

        $whereCom[] = ['comid', '=', $this->userinfo['comid']];
        $whereCom[] = ['deleted_user', '=', 0];
        $whereTurn[] = ['status','=', 1];
        $whereTurn[] = ['isturn','=', 1];

        $ListWhereChan = [];
        $WhereChan = [];
        if($chanid_pid > 0){
            $ListWhereChan[] = ['acc.chanid','in', $chanid_son];
            $WhereChan[] = ['chanid','in', $chanid_son];
        }
        $data['account_shou'] = AccountListModel::alias('list')
            ->join('account acc','acc.id=list.accid')
            ->where(['list.comid'=>$this->userinfo['comid']])
            ->where('list.type','in', [1,2,8,9,11,14])
            ->where('list.dt','between', [$stadt, $enddt])
            ->where($ListWhereChan)->sum('money');
        $data['account_zhi'] = AccountListModel::alias('list')
            ->join('account acc','acc.id=list.accid')
            ->where(['list.comid'=>$this->userinfo['comid']])
            ->where('list.type','not in', [1,2,8,9,11,14])
            ->where('list.dt','between', [$stadt, $enddt])
            ->where($ListWhereChan)->sum('money');

        $data['piao_kai'] = PiaokaiModel::where($whereCom)->where($WhereChan)->where($whereTurn)
            ->where('dt','between', [$stadt, $enddt])->sum('money');
        $data['piao_shou'] = PiaoShouModel::where($whereCom)->where($WhereChan)->sum('money');



        $daizhi = [];
        //3-项目支付 4-费用报销 5-日常付款 6-内包付款 7-保证金缴纳 10保证金返还 13-借支 15-公司费用报销
        $info = PaymentEnginModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>3,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '项目付款（供应商）';
                $v['laipage'] = 'payment_engin';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = PaymentBaoModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>4,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '项目费用报销';
                $v['laipage'] = 'payment_bao';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = PaymentDayModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>5,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '日常付款';
                $v['laipage'] = 'payment_day';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = PaymentNeifuModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>6,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '项目内包付款';
                $v['laipage'] = 'payment_neifu';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = EnginBondJiaoModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>7,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '项目保证金缴纳';
                $v['laipage'] = 'engin_bond_jiao';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = EnginBondFanModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>10,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '项目保证金返还';
                $v['laipage'] = 'engin_bond_fan';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = PaymentJieModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>13,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '备用金申请';
                $v['laipage'] = 'payment_jie';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }
        $info = PaymentGongModel::field('id,money,gonger,created_at')->where($whereCom)->where($whereTurn)->where($WhereChan)->select()->toArray();
        foreach ($info as $v){
            $money = AccountListModel::where(['type'=>15,'mid'=>$v['id']])->sum('money');
            if(!$money){
                $v['lai'] = '公司费用报销';
                $v['laipage'] = 'payment_gong';
                $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
                $daizhi[] = $v;
            }
        }

        $dtSort = array_column($daizhi,'created_at');
        array_multisort($dtSort,SORT_DESC,$daizhi);
        $data['daizhi'] = $daizhi;

        $daibao = EnginBondKaiModel::where($whereCom)->where($whereTurn)->where($WhereChan)
            ->where("kaidt is null or kaidt = '0000-00-00' or kaidt = ''")->select()->toArray();
        foreach ($daibao as &$v){
            if($v['jiedt'] != '0000-00-00' && $v['jiedt']){
                $v['jiedt'] = date("Y-m-d H:i", strtotime($v['jiedt']));
            }else{
                $v['jiedt'] = '';
            }
            $bill = FlowBillModel::where(['id'=>$v['billid']])->find();
            $v['sericnum'] = $bill['sericnum'];
            $v['bondtype'] = (new PaymentService())->get_engin_bondtype_name($v['bondtype']);
            $v['hantype'] = (new OptService())->get_base_showname('engin_bond_hantype', $v['hantype']);
        }
        $data['daibao'] = $daibao;



        $daipiao = PiaokaiModel::where($whereCom)->where($whereTurn)->where($WhereChan)
            ->where("dt is null or dt = '0000-00-00' or dt = ''")->order('created_at DESC')->select()->toArray();
        foreach ($daipiao as &$v){
            $v['created_at'] = date("Y-m-d H:i", strtotime($v['created_at']));
            $v['enginname'] = EnginModel::where(['id'=>$v['enginid']])->value('name');
            $v['piaotype'] = (new PiaoService())->get_shou_piaotype_name($v['piaotype']);
        }
        $data['daipiao'] = $daipiao;




        $dairen = IncomeEnginModel::where($whereCom)->where($WhereChan)->where(['mal'=>0])->select()->toArray();
        $data['dairen'] = $dairen;



        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = $chanid;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cai_dong(Request $request){
        $dt = $request->param('dt', '', 'string');
        $dt = empty($dt) ? date('Y-m-d') : $dt;

        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }

        $WhereChan = [];
        if($chanid_pid > 0){
            $WhereChan[] = ['chanid','in', $chanid_son];
        }

        $list = AccountListModel::alias('list')
            ->join('account acc','acc.id=list.accid')
            ->where(['list.comid'=>$this->userinfo['comid']])
            ->where('list.dt','=', $dt)
            ->where($WhereChan)->select()->toArray();
        foreach ($list as &$v){
            $v['type_name'] = in_array($v['type'], [1,2,8,9,11,14]) ? '到账' : '付款';
        }

        $data['data'] = $list;
        $data['isdt'] = $dt;
        return outputJson(2000,lang('code_2000'),$data);
    }




    public function cert_chan(Request $request){
        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
            $chanid_son = [];
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }
        if($chanid_pid > 0){
            $where[] = ['chanid','in', $chanid_son];
        }
        $where[] = ['comid', '=', $this->userinfo['comid']];
        $where[] = ['deleted_user', '=', 0];


        $data['person_num'] = CertModel::where($where)->group('personid')->count();
        $data['use_person_num'] = CertPersonModel::where($where)->where(['statueuse'=>2])->count();
        $data['free_person_num'] = CertPersonModel::where($where)->where(['statueuse'=>1])->count();
        $data['cert_num'] = CertModel::where($where)->count();
        $data['cert_bor_num'] = CertModel::where($where)->where(['goes'=>2])->count();
        $data['cert_bid_num'] = CertModel::where($where)->where(['statueuse'=>3])->count();
        $data['cert_bao_num'] = CertModel::where($where)->where('statueuse','in',[5,7])->count();
        $data['cert_suo_num'] = CertModel::where($where)->where('statueuse','=',7)->count();
        $data['cert_free_num'] = CertModel::where($where)->where('statueuse','=',1)->count();


        // 借证申请待处理
        $borrow_data = CertBorrowModel::where($where)->where(['isturn'=>1,'status'=>0,'deleted_user'=>0])
            ->field('flowuser,flowdept,yong,created_at,state')->order('created_at desc')->select()->toArray();
        $data['borrow_num'] = count($borrow_data);
        foreach ($borrow_data as &$item){
            $item['state_name'] = (new CertService())->get_cert_borrow_state_name($item['state']);
        }
        $data['borrow_data'] = $borrow_data;
        unset($item);

        // 逾期为归还证书
        $borlist_data = CertBorrowListModel::alias('list')->field('list.certid,bor.flowuser,bor.qudt,bor.yudt')
            ->leftJoin('cert_borrow bor','bor.id = list.mid')
            ->where("list.guidt = '' or list.guidt is null or list.guidt ='0000-00-00'")
            ->where(['bor.state'=>1,'bor.status'=>1,'bor.isturn'=>1,'list.type'=>1])
            ->where(['bor.deleted_user'=>0,'bor.comid'=>$this->userinfo['comid']])
            ->order('bor.qudt asc')
            ->select()->toArray();
        $data['borlist_num'] = count($borlist_data);
        foreach ($borlist_data as &$item){
            $item['cert_name'] = CertModel::where('id',$item['certid'])->value('name');
            $cert_major = CertModel::where('id',$item['certid'])->value('major');
            $cert_major = str_replace('[]',',',$cert_major);
            if(!empty($cert_major)){
                $item['cert_name'] = $item['cert_name'].' / '.$cert_major;
            }
            $item['day'] = get_time_slot_days($item['yudt'],date('Y-m-d'));
        }
        $data['borlist_data'] = $borlist_data;
        unset($item);

        // 投标用证待处理
        $biduse_data = CertUseBidModel::where($where)->where(['isturn'=>1,'status'=>0,'deleted_user'=>0])
            ->field('flowuser,flowdept,bidid,dt')
            ->select()->toArray();
        $data['biduse_num'] = count($biduse_data);
        foreach ($biduse_data as &$item){
            $item['name'] = BidModel::where('id',$item['bidid'])->value('name');
        }
        $data['biduse_data'] = $biduse_data;
        unset($item);

        // 报建用证待处理
        $enginuse_data = CertUseModel::where($where)->where(['isturn'=>1,'status'=>0,'deleted_user'=>0])
            ->field('flowuser,flowdept,enginid,dt')
            ->select()->toArray();
        $data['enginuse_num'] = count($enginuse_data);
        foreach ($enginuse_data as &$item){
            $item['name'] = EnginModel::where('id',$item['enginid'])->value('name');
        }
        $data['enginuse_data'] = $enginuse_data;
        unset($item);


        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = $chanid;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function cert_dong(Request $request){
        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }

        $where[] = ['comid', '=', $this->userinfo['comid']];
        $where[] = ['deleted_user', '=', 0];
        if($chanid_pid > 0){
            $where[] = ['chanid','in', $chanid_son];
        }

        $cert_data = CertModel::where($where)->select()->toArray();
        $list3 = $list6 = $list9 = $list1 = [];
        foreach ($cert_data as $item){
            $person_name = CertPersonModel::where('id',$item['personid'])->value('name');
            $expiry_dt = array_filter(explode('[]',$item['expiry_dt']));
            $major = array_filter(explode('[]',$item['major']));
            if($expiry_dt){
                $date = date('Y-m-d');
                foreach ($expiry_dt as $k => $dt){
                    $dt3 = date('Y-m-d',strtotime($dt."-30 day"));
                    $dt6 = date('Y-m-d',strtotime($dt."-60 day"));
                    $dt9 = date('Y-m-d',strtotime($dt."-90 day"));
                    if($date >= $dt3 && $date <= $dt){
                        if(isset($major[$k])) {
                            $list3[$dt][] = $item['name'] . '[' . $major[$k] . ']' . $person_name;
                        }
                    }
                    if($date >= $dt6 && $date <= $dt){
                        if(isset($major[$k])) {
                            $list6[$dt][] = $item['name'] . '[' . $major[$k] . ']' . $person_name;
                        }
                    }
                    if($date >= $dt9 && $date <= $dt){
                        if(isset($major[$k])) {
                            $list9[$dt][] = $item['name'] . '[' . $major[$k] . ']' . $person_name;
                        }
                    }
                    if($date > $dt){
                        if(isset($major[$k])){
                            $list1[$dt][] = $item['name'].'['.$major[$k].']'.$person_name;
                        }
                    }
                }
            }
        }

        ksort($list3);
        ksort($list6);
        ksort($list9);
        ksort($list1);
        $data['list3'] = $list3;
        $data['list6'] = $list6;
        $data['list9'] = $list9;
        $data['list1'] = $list1;
        return outputJson(2000,lang('code_2000'),$data);
    }




    public function bid_chan(Request $request){
        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
            $chanid_son = [];
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }
        if($chanid_pid > 0){
            $where[] = ['chanid','in', $chanid_son];
        }
        $where[] = ['comid', '=', $this->userinfo['comid']];
        $where[] = ['deleted_user', '=', 0];


        $data['pool_num'] = BidPoolModel::where($where)->count();
        $data['shen_num'] = BidModel::where($where)->where(['status'=>0,'isturn'=>1])->count();
        $data['bid_num'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1,'state'=>1])->count();
        $data['win_num'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1,'state'=>3])->count();
        $data['nowin_num'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1,'state'=>6])->count();


        // 最近截标项目
        $data['bidjie_data'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1])->where('enddt_tou','>=',date('Y-m-d'))->order('enddt_tou asc')
            ->field('name,business,zhaoname,enddt_tou')->limit(5)->select()->toArray();
        foreach ($data['bidjie_data'] as &$item){
            $item['business'] = $item['business'] == 1 ? '自营' : '联营';
        }
        unset($item);

        // 最近待开标项目
        $data['biddai_data'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1])->where(['state'=>1])->where('kaidt','>=',date('Y-m-d'))
            ->order('kaidt asc')->field('name,business,zhaoname,kaidt')->limit(5)->select()->toArray();

        // 最近已开标项目
        $data['bidkai_data'] = BidModel::where($where)->where(['status'=>1,'isturn'=>1])->where('state','in',[3,6])
            ->where('kaidt','<=',date('Y-m-d'))
            ->order('kaidt asc')->field('name,business,zhaoname,status,state,isqi')->limit(5)->select()->toArray();
        foreach ($data['bidkai_data'] as &$item){
            $item['business'] = $item['business'] == 1 ? '自营' : '联营';
            $item['result'] = (new BidService())->get_bid_state_name_wen($item);
        }
        unset($item);

        // 最近中标项目
        $data['bidwin_data'] = BidModel::where($where)->where('kai_windt','<=',date('Y-m-d'))
            ->where(['status'=>1,'isturn'=>1])->where('state','=',3)
            ->order('kai_windt desc')->field('name,business,zhaoname,kai_fee')->limit(5)->select()->toArray();
        foreach ($data['bidwin_data'] as &$item){
            $item['business'] = $item['business'] == 1 ? '自营' : '联营';
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();
        $data['is_chanid'] = $chanid;
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function bid_dong(Request $request){
        $dt = $request->param('dt', '', 'string');
        $dt = empty($dt) ? date('Y-m-d') : $dt;

        $chanid = $request->param('chanid', 0, 'int');
        if(!$chanid){
            $chanid = DeptModel::where(['comid'=>$this->userinfo['id'],'pid'=>0])->value(('id'));
            $chanid_pid = 0;
            $chanid_son = [];
        }else{
            $chanid_pid = DeptModel::where(['id'=>$chanid])->value(('pid'));
            $chanid_son = (new UserService())->get_chansonid($chanid);
        }
        if($chanid_pid > 0){
            $where[] = ['chanid','in', $chanid_son];
        }

        $where[] = ['deleted_user','=', 0];
        $where[] = ['comid','=',$this->userinfo['comid']];
        $where[] = ['created_at','between', [$dt.' 00:00:00', $dt.' 23:59:59']];

        $flow_list = [];
        $list = FlowBillModel::where($where)->where(['isturn'=>1])->where('status','in',[0,1])
            ->order('created_at DESC')->where('modenum','bid')->select()->toArray();
        foreach ($list as $v){
            $v['created_time'] = date("H:i", strtotime($v['created_at']));
            $v['name'] = $v['summary'];
            $flow_list[] = $v;
        }

        $list = FlowBillModel::where($where)->where(['isturn'=>1])->where('status','in',[0,1])
            ->order('created_at DESC')->where('modenum','in',['engin_bond_jiao','engin_bond_fan','engin_bond_kai'])
            ->where('bidid','>',0)->select()->toArray();
        foreach ($list as $v){
            $v['created_time'] = date("H:i", strtotime($v['created_at']));
            $v['name'] = $v['summary'];
            $flow_list[] = $v;
        }

        $list = FlowBillModel::where($where)->where(['isturn'=>1])->where('status','in',[0,1])
            ->order('created_at DESC')->where(['modenum'=>'cert_use'])
            ->select()->toArray();
        foreach ($list as $v){
            $type = CertUseModel::where('id',$v['mid'])->value('type');
            if($type == 1){
                $v['created_time'] = date("H:i", strtotime($v['created_at']));
                $v['name'] = $v['summary'];
                $flow_list[] = $v;
            }
        }

        $data['data'] = $flow_list;
        $data['isdt'] = $dt;
        return outputJson(2000,lang('code_2000'),$data);
    }


}















